package cn.sczhyt.modules.szhy.service.impl;

import cn.sczhyt.modules.szhy.dao.MeetingUserDao;
import cn.sczhyt.modules.szhy.entity.MeetingEntity;
import cn.sczhyt.modules.szhy.entity.MeetingUserEntity;
import cn.sczhyt.modules.szhy.service.MeetingService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.sczhyt.common.utils.PageUtils;
import cn.sczhyt.common.utils.Query;

import cn.sczhyt.modules.szhy.dao.MeetingHotelDao;
import cn.sczhyt.modules.szhy.entity.MeetingHotelEntity;
import cn.sczhyt.modules.szhy.service.MeetingHotelService;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author Admin
 */
@Service("meetingHotelService")
public class MeetingHotelServiceImpl extends ServiceImpl<MeetingHotelDao, MeetingHotelEntity> implements MeetingHotelService {

    @Autowired
    private MeetingService meetingService;

    @Autowired
    private MeetingUserDao meetingUserDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MeetingHotelEntity> page = this.page(
                new Query<MeetingHotelEntity>().getPage(params),
                new QueryWrapper<MeetingHotelEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<MeetingHotelEntity> getByMeetingId(String meetingId) {
        QueryWrapper<MeetingHotelEntity> wrapper = new QueryWrapper<MeetingHotelEntity>()
                .eq("meeting_id", meetingId)
                .orderByAsc("start_date");

        List<MeetingHotelEntity> meetingHotelEntities = baseMapper.selectList(wrapper);


        MeetingEntity meetingEntity = meetingService.getById(meetingId);

        if (meetingEntity == null) {
            return null;
        }

        List<MeetingHotelEntity> collect = meetingHotelEntities.stream().map(entity -> {
            entity.setMeetingName(meetingEntity.getMeetingName());
            return entity;
        }).collect(Collectors.toList());

        return collect;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveMeetingHotel(MeetingHotelEntity meetingHotel) {

//        JSONArray jsonArray = JSONArray.fromObject(meetingHotel.getHotelGuests());
//
//        System.out.println("jsonArray" + jsonArray);
//        String meetingId = meetingHotel.getMeetingId();
//
//        if (!jsonArray.isEmpty()) {
//            for (int i = 0; i < jsonArray.size(); i++) {
//                String attendeeId = jsonArray.getJSONObject(i).getString("attendeeId");
//
//                UpdateWrapper<MeetingUserEntity> updateWrapper = new UpdateWrapper<>();
//                updateWrapper.eq("meeting_user_id",attendeeId).eq("meeting_id",meetingId);
//
//                MeetingUserEntity meetingUserEntity = new MeetingUserEntity();
//
//                meetingUserDao.update(meetingUserEntity,updateWrapper);
//
//            }
//        }

        this.baseMapper.insert(meetingHotel);
    }

}